iT邦幫忙

2024 iThome 鐵人賽

DAY 14
0
Mobile Development

用Flutter Flame做遊戲!Live!系列 第 16

多種碰撞條件設定...

  • 分享至 

  • xImage
  •  

之前是使用「Filter」來設定「BodyComponent可以發生碰撞的對象」。
但其實只要將BodyComponent設定為「Sensor」,就可以讓它處於「偵測接觸,但無碰撞」的狀態。——biginContact與endContact這次有正常運作了!

既然biginContact與endContact有正常運作,那就可以進一步改造之前的做法,讓它變得效能更好!

原本是將「判斷」交由「Ball」來處理,但改為「Hole」處理。

首先要廢除下面這段「Ball」的程式碼:


  List<Hole> holes = [];
  addHole(Hole hole){
    holes.add(hole);
  }

  removeHole(Hole hole){
    holes.remove(hole);
  }

然後修改「Hole」:


  List<Ball> balls = [];

  @override
  void beginContact(Object other, Contact contact) {

    if(other is Ball){
      if(other.radius <= radius) {
        balls.add(other);
      }
    }

    super.beginContact(other, contact);
  }

  @override
  void endContact(Object other, Contact contact) {
    if(other is Ball){
      if(other.radius <= radius) {
        balls.remove(other);
      }
    }

    super.beginContact(other, contact);
  }


  //update
    if(balls.length > 0){
        for(Ball ball in balls){
          if((ball.body.position - body.position).length < radius){
            world.remove(ball);
          }
        }
      }

這樣就能省去許許多多的麻煩了!


本意是想要藉由在「Filter」的「maskBits」設定多組條件來完成。


  fixtureDef.filter.maskBits = 條件ㄧ | 條件二 | 條件三 ... ;

但是此路不通!(沒有發生正常效果,似乎只有條件一會起作用。)
其實這條路直得繼續研究下去,但是這有點偏離主題,所以做、但是先完成主要目標吧!


上一篇
回題!繼續畫圖!
下一篇
Path和Canvas.clip
系列文
用Flutter Flame做遊戲!Live!26
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言